package com.baidu.bulls.stock.gateway.filter;

import lombok.extern.log4j.Log4j2;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.http.HttpHeaders;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

@Component
@Log4j2
public class StockRequestGlobalFilter implements GlobalFilter , Ordered {

    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        ServerHttpRequest request = exchange.getRequest();
        String authorization = request.getHeaders().getFirst("Authorization");
        //请求转发携带头信息
        if (!StringUtils.isEmpty(authorization)){
            log.info("enter stockRequestGlobalFilter filter method:{}",authorization);
            exchange.getRequest().mutate().header("Authorization",authorization);
        }

        return chain.filter(exchange.mutate().build());
    }

    @Override
    public int getOrder() {
        return -1000;
    }
}
